package com.example.tutorial.ultils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUltil {

	/**
	 * <p>
	 * Compare 2 date.
	 * </p>
	 * 
	 * @param Date1
	 *            date 1
	 * @param Date2
	 *            date 2
	 * @return 1 : if date1 > date2 <br/>
	 *         0 : if date1 == date2 <br/>
	 *         -1 : if date 1 < date2 <br/>
	 */
	public static int compare2Date(final Date date1, final Date date2) {
		int result = 0;
		final int distance = distanceBetween2Dates(date1, date2);
		if (distance > 0) {
			result = -1;
		} else if (distance < 0) {
			result = 1;
		} else {
			result = 0;
		}
		return result;
	}

	/**
	 * <p>
	 * Calculate distance (in day) between 2 Date object.
	 * </p>
	 * 
	 * @param from
	 *            From date
	 * @param to
	 *            To date
	 * @return Number of days different -1 if from > to
	 */
	public static int distanceBetween2Dates(final Date from, final Date to) {
		final Calendar calFrom = new GregorianCalendar();
		calFrom.setTime(from);
		final Calendar calTo = new GregorianCalendar();
		calTo.setTime(to);
		final long dis = calTo.getTimeInMillis() - calFrom.getTimeInMillis();
		if (dis < 0) {
			return -1;
		}
		final long dateInMiliSeconds = 1000 * 60 * 60 * 24;
		return (Math.round((dis / (dateInMiliSeconds))));

	}
}
